import { getCates } from "../../request/api.js"
const state = {
    //一级分类
    listOne: [],
    //二级分类
    listTow:[]
}
const mutations = {
    changeOne(state, val) {
        state.listOne = val
    },
    changeTwo(state, val) {
        state.listTow=val
    }
}
const actions = {
    asyncChangeOne(context) {
        getCates().then(res => {
            //给一级分类赋值
            context.commit("changeOne", res.data.list);
            //给二级分类赋值 默认渲染一级分类下的第一个数据
            context.commit("changeTwo", res.data.list[0].children);
        } )
    },
    asyncChangeTwo(context, index) {
        //通过index来判断你点的是哪一项 就给二级分类赋值哪一项的数据
        context.commit("changeTwo",context.state.listOne[index].children)
    }
}

const getters = {
    one(state) {
        return state.listOne
    },
    two(state) {
        return state.listTow
    }
}
export default {
    state, mutations, actions, getters,
    namespaced: true
}